//
//  ResultViewController.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/7/29.
//

import UIKit

class ResultViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var homeBtn: UIButton!
    @IBOutlet weak var restartBtn: UIButton!
    @IBOutlet weak var nextBtn: UIButton!
    
    var data: Array<NSMutableAttributedString>?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        homeBtn.setBackgroundImage(UIImage.imageWithColor(UIColor(hexString: "#FBFDFC")), for: .normal)
        restartBtn.setBackgroundImage(UIImage.imageWithColor(UIColor(hexString: "#FBFDFC")), for: .normal)
        nextBtn.setBackgroundImage(UIImage.imageWithColor(UIColor(hexString: "#FBFDFC")), for: .normal)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    }

    @IBAction func clickHome(_ sender: Any) {
        navigationController?.popToRootViewController(animated: true)
    }
    
    @IBAction func clickRestart(_ sender: Any) {
        NotificationCenter.default.post(name: .restart, object: nil)
        navigationController?.popViewController(animated: true)
    }
    
    @IBAction func clickNext(_ sender: Any) {
        NotificationCenter.default.post(name: .restart, object: nil, userInfo: [kNotiKey_IsNext: true])
        navigationController?.popViewController(animated: true)
    }
}

extension ResultViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        var config = UIListContentConfiguration.cell()
        config.attributedText = data?[indexPath.row]
        cell.contentConfiguration = config
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
